/*
 * Logger.java
 *
 * 功  能：
 * 类  名：Logger
 * 版权所有：中兴（沈阳）金融科技有限公司
 *
 *   版本        变更日期             作者      变更内容
 * ────────────────────────────
 *   V1.0        2016年6月7日    dong.jun      新建
*/
package cn.com.zte.ngbc.sys.logging;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import org.apache.logging.log4j.spi.ExtendedLogger;

/**
 * 描述：
 * 
 * @author dong.jun
 * @version 1.0
 */
public class LogManager {
	private final ExtendedLogger logger;
	public String FQCN = LogManager.class.getName();
	public Marker EVENT_MARKER = MarkerManager.getMarker("EVENT");

	private LogManager(String name) {
		logger = org.apache.logging.log4j.LogManager.getContext(false).getLogger(name);
	}

	private LogManager(Class<?> clazz) {
		this(clazz.getName());
	}

	public static LogManager getLogger(Class<?> clazz) {
		return new LogManager(clazz);
	}

	public static LogManager getLogger(String name) {
		return new LogManager(name);
	}

	public static SysLog getSysLog(String name) {
		return new SysLog(LogManager.getLogger(name));
	}

	public static SysLog getSysLog(Class<?> clazz) {
		return new SysLog(LogManager.getLogger(clazz));
	}

	public static BizLog getBizLog(String name) {
		return new BizLog(LogManager.getLogger(name));
	}

	public static BizLog getBizLog(Class<?> clazz) {
		return new BizLog(LogManager.getLogger(clazz));
	}

	public void trace(Object message) {
		if (logger.isTraceEnabled()) {
			forcedLog(logger, Level.TRACE, message);
		}
	}

	public void trace(Object message, Throwable t) {
		if (logger.isTraceEnabled()) {
			forcedLog(logger, Level.TRACE, message, t);
		}
	}

	public void trace(String pattern, Object... arguments) {
		if (logger.isTraceEnabled()) {
			forcedLog(logger, Level.TRACE, format(pattern, arguments));
		}
	}

	public void trace(String pattern, Throwable t, Object... arguments) {
		if (logger.isTraceEnabled()) {
			forcedLog(logger, Level.TRACE, format(pattern, arguments), t);
		}
	}

	public void debug(Object message) {
		if (logger.isDebugEnabled()) {
			forcedLog(logger, Level.DEBUG, message);
		}
	}

	public void debug(Object message, Throwable t) {
		if (logger.isDebugEnabled()) {
			forcedLog(logger, Level.DEBUG, message, t);
		}
	}

	public void debug(String pattern, Object... arguments) {
		if (logger.isDebugEnabled()) {
			forcedLog(logger, Level.DEBUG, format(pattern, arguments));
		}
	}

	public void debug(String pattern, Throwable t, Object... arguments) {
		if (logger.isDebugEnabled()) {
			forcedLog(logger, Level.DEBUG, format(pattern, arguments), t);
		}
	}

	public void info(Object message) {
		if (logger.isInfoEnabled()) {
			forcedLog(logger, Level.INFO, message);
		}
	}

	public void info(Object message, Throwable t) {
		if (logger.isInfoEnabled()) {
			forcedLog(logger, Level.INFO, message, t);
		}
	}

	public void info(String pattern, Object... arguments) {
		if (logger.isInfoEnabled()) {
			forcedLog(logger, Level.INFO, format(pattern, arguments));
		}
	}

	public void info(String pattern, Throwable t, Object... arguments) {
		if (logger.isInfoEnabled()) {
			forcedLog(logger, Level.INFO, format(pattern, arguments), t);
		}
	}

	public void warn(Object message) {
		if (logger.isEnabled(Level.WARN)) {
			forcedLog(logger, Level.WARN, message);
		}
	}

	public void warn(Object message, Throwable t) {
		if (logger.isEnabled(Level.WARN)) {
			forcedLog(logger, Level.WARN, message, t);
		}
	}

	public void warn(String pattern, Object... arguments) {
		if (logger.isEnabled(Level.WARN)) {
			forcedLog(logger, Level.WARN, format(pattern, arguments));
		}
	}

	public void warn(String pattern, Throwable t, Object... arguments) {
		if (logger.isEnabled(Level.WARN)) {
			forcedLog(logger, Level.WARN, format(pattern, arguments), t);
		}
	}

	public void error(Object message) {
		if (logger.isEnabled(Level.ERROR)) {
			forcedLog(logger, Level.ERROR, message);
		}
	}

	public void error(Object message, Throwable t) {
		if (logger.isEnabled(Level.ERROR)) {
			forcedLog(logger, Level.ERROR, message, t);
		}
	}

	public void error(String pattern, Object... arguments) {
		if (logger.isEnabled(Level.ERROR)) {
			forcedLog(logger, Level.ERROR, format(pattern, arguments));
		}
	}

	public void error(String pattern, Throwable t, Object... arguments) {
		if (logger.isEnabled(Level.ERROR)) {
			forcedLog(logger, Level.ERROR, format(pattern, arguments), t);
		}
	}

	public void fatal(Object message) {
		if (logger.isEnabled(Level.FATAL)) {
			forcedLog(logger, Level.FATAL, message);
		}
	}

	public void fatal(Object message, Throwable t) {
		if (logger.isEnabled(Level.FATAL)) {
			forcedLog(logger, Level.FATAL, message, t);
		}
	}

	public void fatal(String pattern, Object... arguments) {
		if (logger.isEnabled(Level.FATAL)) {
			forcedLog(logger, Level.FATAL, format(pattern, arguments));
		}
	}

	public void fatal(String pattern, Throwable t, Object... arguments) {
		if (logger.isEnabled(Level.FATAL)) {
			forcedLog(logger, Level.FATAL, format(pattern, arguments), t);
		}
	}

	public void assertLog(boolean assertion, String message) {
		if (!assertion) {
			forcedLog(logger, Level.ERROR, message);
		}
	}

	private void forcedLog(ExtendedLogger logger, Level level, Object message) {
		logger.logIfEnabled(FQCN, level, EVENT_MARKER, message, null);
	}

	private void forcedLog(ExtendedLogger logger, Level level, Object message, Throwable t) {
		logger.logIfEnabled(FQCN, level, EVENT_MARKER, message, t);
	}

	public String format(String pattern, Object... arguments) {
		return String.format(pattern, arguments);
	}
}